
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.wyzz.hr.bo.IJobgradeBO;
import com.wyzz.hr.bo.IJobgradeDAO;
import com.wyzz.hr.pojo.Jobgrade;
import com.wyzz.hr.util.MyTools;


public class JobgradeBOImpl implements IJobgradeBO {

	public JobgradeBOImpl() {
		dao = null;
	}

	public Jobgrade loadJobgrade(String id) {
		return (Jobgrade) dao.loadObject(com.wyzz.hr.pojo.Jobgrade.class, id,
				null, new boolean[0]);
	}

	public List FindAllJobgrade() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Jobgrade.class);
		dc.addOrder(Order.asc(Jobgrade.PROP_JOB_GRADE_SORT_ID));
		return dao.findByCriteria(dc);
	}

	public boolean addJobgrade(Jobgrade jobgrade) {
		String trimmedJobGradeNo = jobgrade.getJobGradeNo().trim();
		List old = dao.exeHqlList((new StringBuilder()).append(
				" from Jobgrade where jobGradeNo ='").append(trimmedJobGradeNo)
				.append("'").toString());
		if (old == null || old.size() == 0) {
			String trimmedJobGradeName = jobgrade.getJobGradeName().trim();
			jobgrade.setJobGradeLevel(jobgrade.getJobGradeLevel());
			jobgrade.setJobGradeName(trimmedJobGradeName);
			jobgrade.setJobGradeNo(trimmedJobGradeNo);
			jobgrade.setJobGradeSortId(Integer.valueOf(0));
			UUID uuid = UUID.randomUUID();
			jobgrade.setId(uuid.toString());
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_MRP, jobgrade
					.getJobGradeMrp(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF1, jobgrade
					.getJobGradeRef1(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF2, jobgrade
					.getJobGradeRef2(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF3, jobgrade
					.getJobGradeRef3(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF4, jobgrade
					.getJobGradeRef4(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF5, jobgrade
					.getJobGradeRef5(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF6, jobgrade
					.getJobGradeRef6(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF7, jobgrade
					.getJobGradeRef7(), jobgrade.getId(), MyTools.BIGDECIMAL);
			jobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF8, jobgrade
					.getJobGradeRef8(), jobgrade.getId(), MyTools.BIGDECIMAL);
			dao.saveObject(jobgrade);
			return true;
		}
		try {
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateJobgrade(Jobgrade jobgrade) {
		String trimmedJobGradeNo = jobgrade.getJobGradeNo().trim();
		List old = dao.exeHqlList((new StringBuilder()).append(
				" from Jobgrade where jobGradeNo ='").append(trimmedJobGradeNo)
				.append("' and id <> '").append(jobgrade.getId().trim())
				.append("'").toString());
		if (old == null || old.size() == 0) {
			Jobgrade oldJobgrade = (Jobgrade) dao.loadObject(
					com.wyzz.hr.pojo.Jobgrade.class, jobgrade.getId(), null,
					new boolean[0]);
			String trimmedJobGradeName = jobgrade.getJobGradeName().trim();
			if (jobgrade.getJobGradeMrp() != null)
				oldJobgrade.setEncry("jobGradeMrp", jobgrade.getJobGradeMrp(),
						jobgrade.getId(), MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeMrp(null);
			if (jobgrade.getJobGradeRef1() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF1, jobgrade
						.getJobGradeRef1(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef1(null);
			if (jobgrade.getJobGradeRef2() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF2, jobgrade
						.getJobGradeRef2(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef2(null);
			if (jobgrade.getJobGradeRef3() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF3, jobgrade
						.getJobGradeRef3(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef3(null);
			if (jobgrade.getJobGradeRef4() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF4, jobgrade
						.getJobGradeRef4(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef4(null);
			if (jobgrade.getJobGradeRef5() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF5, jobgrade
						.getJobGradeRef5(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef5(null);
			if (jobgrade.getJobGradeRef6() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF6, jobgrade
						.getJobGradeRef6(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef6(null);
			if (jobgrade.getJobGradeRef7() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF7, jobgrade
						.getJobGradeRef7(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef7(null);
			if (jobgrade.getJobGradeRef8() != null)
				oldJobgrade.setEncry(Jobgrade.PROP_JOB_GRADE_REF8, jobgrade
						.getJobGradeRef8(), jobgrade.getId(),
						MyTools.BIGDECIMAL);
			else
				oldJobgrade.setJobGradeRef8(null);
			oldJobgrade.setJobGradeNo(trimmedJobGradeNo);
			oldJobgrade.setJobGradeLevel(jobgrade.getJobGradeLevel());
			oldJobgrade.setJobGradeName(trimmedJobGradeName);
			dao.updateObject(oldJobgrade);
			return true;
		} else {
			return false;
		}
	}

	public boolean delJobgrade(Class name, String id) {
		Jobgrade jobgrade;
		jobgrade = (Jobgrade) dao.loadObject(name, id, null, new boolean[0]);
		if (jobgrade == null)
			return false;
		List check = new ArrayList();
		check = dao.exeHqlList((new StringBuilder()).append(
				"select count(*) from Empsalaryadj where esaJobgradeCur = '")
				.append(id).append("'or esaJobgradePro = '").append(id).append(
						"'").toString());
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			return false;
		check = dao.exeHqlList((new StringBuilder()).append(
				"select count(*) from Empsalarypay where espJobgrade = '")
				.append(id).append("'").toString());
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			return false;
		check = dao.exeHqlList((new StringBuilder()).append(
				"select count(*) from Empsalaryconfig where escJobgrade = '")
				.append(id).append("'").toString());
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			return false;
		check = dao.exeHqlList((new StringBuilder()).append(
				"select count(*) from JobTitle where jobtitleDefaultJg.id = '")
				.append(id).append("'").toString());
		if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
			return false;
		try {
			dao.deleteObject(jobgrade);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public void saveJobGradeIdByBatch(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			dao.exeHql((new StringBuilder()).append(
					"update Jobgrade set jobGradeSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	public IJobgradeDAO getDao() {
		return dao;
	}

	public void setDao(IJobgradeDAO dao) {
		this.dao = dao;
	}

	private IJobgradeDAO dao;
}
